package org.hxyjs.dao.impl;

import org.hxyjs.dao.AdminChefDao;
import org.hxyjs.entity.AdminChef;
import org.hxyjs.mapper.AdminChefMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("adminChefDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class AdminChefDaoImpl implements AdminChefDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private AdminChefMapper adminChefMapper;

    @Override
    public List<AdminChef> getAdminChefByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) adminChefMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return adminChefMapper.getAdminChefByPage(map);
    }


    public List<AdminChef> getAllAdminChefs() throws Exception {
        return adminChefMapper.selectByExample(null);
    }

    @Override
    public AdminChef getAdminChefByAcno(String acno) throws Exception {
        return adminChefMapper.selectByPrimaryKey(acno);
    }


    @Override
    public Integer deleteAdminChefByAcno(String acno) {
        return adminChefMapper.deleteByPrimaryKey(acno);
    }

    @Override
    public Integer updateAdminChefByAcno(AdminChef adminChef) {

        return adminChefMapper.updateByPrimaryKey(adminChef);
    }

    @Override
    public Integer addAdminChef(AdminChef adminChef) {
        adminChef.setAcno(initId());
        return adminChefMapper.insertSelective(adminChef);
    }

    public String initId() {
        String maxAcno = adminChefMapper.getMaxAcno();
        return GenNumTools.initId("700200022021050619", 4, maxAcno);
    }

}